@include('base.loading_admin')
<style>
    .state_play {

    }

    .fa {
        cursor: pointer;
    }
</style>
<table class="table table-striped">
    <thead>
    <tr>
        <th>订单编号</th>
        <th>创建者</th>
        <th>创建时间</th>
        <th>订单总额</th>
        <th>订单内容</th>
        <th>订单状态</th>
        <th>订单操作</th>
    </tr>
    </thead>
    <tbody>
    @foreach($parent_order as $key => $value)
        <tr>
            <td>{{$value["parent_order_id"]}}</td>
            <td>{{$value["user"]["nickname"]}}</td>
            <td>{{$value["created_at"]}}</td>
            <td>{{$value["all_price"]}}</td>
            <td>
                <div class="order_list">
                    @foreach($value["data"] as $k => $v)
                        <p class="order_list_box">
                            <span>{{$v["commodity_name"]}}</span>
                            <img src="{{$v["info_img_url"]}}" alt="" style="width: 50px;height: 50px;">
                            <span>{{$v["commodity_color_name"]}},{{$v["commodity_size_name"]}} x{{$v["number"]}}</span>
                        </p>
                    @endforeach
                </div>
            </td>
            <td><span class="btn btn-sm btn-default state_play" data-state="{{$value["state"]}}"
                      data-order-id="{{$value["parent_order_id"]}}">{{$value["state_name"]}}</span></td>
            <td>
                <a href="/admin/order/{{$value["parent_order_id"]}}/edit"><i class="fa fa-edit" aria-hidden="true"></i></a>
                <i class="fa fa-close close_order" data-order-id="{{$value["parent_order_id"]}}"
                   aria-hidden="true"></i>
                <i class="fa fa-trash-o delete_order" data-order-id="{{$value["parent_order_id"]}}"
                   aria-hidden="true"></i>

            </td>
        </tr>
    @endforeach
    </tbody>
</table>
<script>
    var state_admin = <?=json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)?>;
    loading.hide()
    $(".state_play").on("click", function () {
        loading.show()
        var th = $(this)
        var state = parseInt(th.attr("data-state"))
        var parent_order_id = parseInt(th.attr("data-order-id"))
        console.log(state);
        switch (state) {
            case 1:
            case 4:
                add_state(th, parent_order_id, state)
                break
            default:
                loading.hide()
                break
        }
    })

    function add_state(th, id, state) {
        var data = {
            id: id,
            state: state,
            _token: csrf_token
        }
        $.post("/admin/order/api_order/add_state", data, function (e) {
            if (e.errorCode == 0) {
                change_state_text(th, e.data.state)
            } else {
                alert(e.errorMsg);
            }
            loading.hide()
        });
    }

    function change_state_text(th, state) {
        th.attr("data-state", state)
        th.text(state_admin[state])
    }

    $(".close_order").on("click", function () {
        loading.show()
        var th = $(this)
        var parent_order_id = parseInt(th.attr("data-order-id"))
        close_order(th, parent_order_id)
    })

    function close_order(th, id) {
        var data = {
            id: id,
            _token: csrf_token
        }
        $.post("/admin/order/api_order/close_order", data, function (e) {
            if (e.errorCode == 0) {
                // change_state_text(th, e.data.state)
            } else {
                alert(e.errorMsg);
            }
            loading.hide()
        });
    }

    $(".delete_order").on("click", function () {
        loading.show()
        var th = $(this)
        var parent_order_id = parseInt(th.attr("data-order-id"))
        delete_order(th, parent_order_id)
    })

    function delete_order(th, id) {
        var data = {
            id: id,
            _token: csrf_token
        }
        $.post("/admin/order/api_order/delete_order", data, function (e) {
            if (e.errorCode == 0) {
                // change_state_text(th, e.data.state)
            } else {
                alert(e.errorMsg);
            }
            loading.hide()
        });
    }
</script>